package com.zuoqi.easywork.userManage.controller;

import com.github.pagehelper.Page;
import com.zuoqi.common.utils.IMap;
import com.zuoqi.common.utils.PageView;
import com.zuoqi.common.utils.ReturnMapUtils;
import com.zuoqi.common.utils.SessionUtil;
import com.zuoqi.easywork.userManage.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/userManage")
public class userManage {
    @Autowired
    private UserService userService;
    @RequestMapping(value = "/toPage/userManageView")
    public String userManageView(){
        return "/module/userManage/userManage";
    }
    /**
     * 获得人员列表
     * */
    @RequestMapping(value = "/ajax/getUserList",method = RequestMethod.POST)
    @ResponseBody
    public Map getProjectList(HttpServletRequest request){
        IMap params = new IMap(request);
        Page page = PageView.startPageForTable(params, false);
        List<IMap> projectList = userService.getUserListForManage(params);
        Map<String, Object> result = PageView.getResultForTable(page, projectList);
        return result;
    }
    /**
     * 添加人员
     * */
    @RequestMapping(value = "/toPage/addUserView")
    public String addProjectView(){
        return "/module/userManage/addUser";
    }

    @RequestMapping(value = "/ajax/addUser",method = RequestMethod.POST)
    @ResponseBody
    public Map addProject(HttpServletRequest request){
        IMap params = new IMap(request);
        HttpSession session = request.getSession();
        String userId = SessionUtil.getLoginUserId(session);
        params.put("updateUser",userId);
        int i = userService.addUser(params);
        if (i==1){
            return ReturnMapUtils.businessResult("0000","添加成功！");
        }else {
            return ReturnMapUtils.businessResult("9999","服务器错误，添加失败！");
        }
    }
    /**
     * 员工信息修改
     * */
    @RequestMapping(value = "/toPage/updUserView")
    public String updUserView(HttpServletRequest request,ModelMap modelMap){
        IMap params = new IMap(request);
        IMap thisUser = userService.getUserInfo(params.getString("userId"));
        modelMap.addAttribute("thisUser",thisUser);
        return "/module/userManage/updUserInfo";
    }
    @RequestMapping(value = "/ajax/UpdUser",method = RequestMethod.POST)
    @ResponseBody
    public Map UpdUser(HttpServletRequest request){
        IMap params = new IMap(request);
        HttpSession session = request.getSession();
        String userId = SessionUtil.getLoginUserId(session);
        params.put("updateUser",userId);
        int i = userService.uptUser(params);
        if (i==1){
            return ReturnMapUtils.businessResult("0000","修改成功！");
        }else {
            return ReturnMapUtils.businessResult("9999","服务器错误，修改失败！");
        }
    }
    /**
     * 确定入职
     */
    @RequestMapping(value = "/ajax/doUserIn",method = RequestMethod.POST)
    @ResponseBody
    public Map doUserIn(HttpServletRequest request){
        IMap params = new IMap(request);
        HttpSession session = request.getSession();
        String userId = SessionUtil.getLoginUserId(session);
        params.put("updateUser",userId);
        int i = userService.doUserIn(params);
        if (i==1){
            return ReturnMapUtils.businessResult("0000","操作成功！");
        }else {
            return ReturnMapUtils.businessResult("9999","服务器错误，操作失败！");
        }
    }
    /**
     * 确定离职
     */
    @RequestMapping(value = "/ajax/alreadyLeave",method = RequestMethod.POST)
    @ResponseBody
    public Map alreadyLeave(HttpServletRequest request){
        IMap params = new IMap(request);
        HttpSession session = request.getSession();
        String userId = SessionUtil.getLoginUserId(session);
        params.put("updateUser",userId);
        int i = userService.alreadyLeave(params);
        if (i==1){
            return ReturnMapUtils.businessResult("0000","操作成功！");
        }else {
            return ReturnMapUtils.businessResult("9999","服务器错误，操作失败！");
        }
    }
}
